package org.docmanagement.commons.struts;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.docmanagement.commons.Constants;

/**
 * Created on 14 oct. 2004
 * @author Mektoub
 */
public abstract class BaseDispatchAction extends DispatchAction implements Constants
{
    protected Map<String, String> methodMap = null;
    protected final String classname = getClass().getName();
    /**
     * 
     */
    protected abstract void createMethodMap();
    
    /**
     * @see org.apache.struts.actions.DispatchAction#getMethodName(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String)
     */
    protected String getMethodName(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response,
            String parameter) throws Exception
    {
        String actionName = super.getMethodName(mapping, form, request, response, parameter);
        log.debug("Get real method name for actionName: "+actionName);
        if (methodMap == null)
            createMethodMap();
        String methodName = (String) methodMap.get(actionName);
        log.debug("Method found: "+methodName);
//        if (methodName == null)
//            throw new NoSuchMethodException("No method associated with actionName ["+actionName+"]");
        return methodName;
    }
    /**
     * 
     * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception
    {
		log.info( "Calling action: "+classname+"."+getActionName(mapping, request)+"()" );
//		if (log.isDebugEnabled() && form != null)
//		{
//		    log.debug("with Action form : "+form.toString());
//		}
		ActionForward forward = super.execute(mapping, form, request, response);
		log.info( "Action successfully called: "+classname+"."+getActionName(mapping, request)+"()" );
		return forward;
    }
    /**
     * @return
     */
    protected String getActionName(ActionMapping mapping, HttpServletRequest request)
    {
        return request.getParameter(mapping.getParameter());
    }

    /**
     * 
     * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        return list(mapping, form, request, response);
    }
    /**
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        return mapping.findForward(SUCCESS);
    }
}
